package com.weic.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 切面类
 */
@Aspect
@Component
public class MyAspect {

    /**
     * 前置通知
     * @param joinPoint
     */
    @Before(value = "execution(* com.weic.service.UserService.addUser(..))")
    public void before(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println("Before Pointcut .... and args is "+ Arrays.toString(args));
    }


    /**
     * 后置通知/最终通知
     * 发生异常也能执行
     * @param joinPoint
     */
    @After(value = "execution(* com.weic.service.UserService.addUser(..))")
    public void after(JoinPoint joinPoint){
        System.out.println("After Pointcut ....");
    }

    /**
     * 返回通知，切点方法返回后才调用，发生异常则不执行
     * @param joinPoint
     * @param res
     */
//    @AfterReturning(value = "execution(* com.weic.service.UserService.addUser(..))",returning = "res")
//    public int afterReturning(JoinPoint joinPoint,int res){
//        System.out.println("afterReturning Pointcut .... and res is :"+res);
//        return res;
//    }

    @AfterThrowing(value = "execution(* com.weic.service.UserService.addUser(..))",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        System.out.println("AfterThrowing Pointcut .... and Exception is :"+ex.getMessage());
    }

    @Around(value = "execution(* com.weic.service.UserService.addUser(..))")
    public void around(JoinPoint joinPoint){
        System.out.println("around Pointcut .... ");
    }

}
